/**
 * Copyright(c)2013 Beijing PeaceMap Co.,Ltd.
 * All right reserved. 
 */
package com.pmc.dwa.common.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Types;

import org.apache.commons.lang.StringUtils;

import com.pmc.dwa.common.annotation.PmcColumn;

/**
 * @description 对bean的反射操作
 * @author aokunsang
 * @date 2013-7-3
 */
public class BeanUtils {

	  /** 
     * @param obj 操作的对象 
     * @param att 操作的属性 
     * */
	public static Object getter(Object obj, String att) { 
        try { 
            Method method = obj.getClass().getMethod("get" + StringUtils.capitalize(att)); 
            return method.invoke(obj); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        }
		return null; 
    }
    /**
     * 注入数据
     * @param obj   类的实例
     * @param att   属性名
     * @param value 注入数据内容
     * @param type  返回的数据类型
     * 
     */
	public static void setter(Object obj, String att, Object value, Class<?> type) { 
        try { 
            Method method = obj.getClass().getMethod("set" + StringUtils.capitalize(att), type); 
            method.invoke(obj, value); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        }
    }
	
	/**
	 * 获取某个属性字段的ignore信息
	 * @param field
	 * @return
	 */
	public static boolean getPmcColumnIgnore(Field field){
		Annotation annotation = field.getAnnotation(PmcColumn.class);
		boolean ignore = false;
		if(annotation!=null){
			ignore = ((PmcColumn)annotation).ignore();
		}
		return ignore;
	}
	
	/**
	 * 获取某个属性的Type信息
	 * @param field
	 * @return
	 */
	public static int getPmcColumnType(Field field){
		Annotation annotation = field.getAnnotation(PmcColumn.class);
		int type = Types.VARCHAR;
		if(annotation!=null){
			type = ((PmcColumn)annotation).type();
		}
		return type;
	}
	
	/**
	 * 获取某个属性的数据库中字段名
	 * @param field
	 * @return
	 */
	public static String getPmcColumnName(Field field){
		Annotation annotation = field.getAnnotation(PmcColumn.class);
		String columnName = "";
		if(annotation!=null){
			columnName = ((PmcColumn)annotation).columnName();
		}
		return columnName;
	}
}
